/*
 * Ball.java -- the running ball
 */
package bounce;

import java.awt.Color;
import java.awt.Graphics;

/**
 *
 * @author PANAIMIN
 */
public class Ball {

    public static final int RADIUS = 8;
    public static final int DIAMETER = RADIUS * 2;

    public Ball(int x, int y) {
        _x = x;
        _y = y;
    }

    public int getX()               { return _x; }
    public int getY()               { return _y; }
    public void right(boolean b)    { _right = b; }
    public void down(boolean b)     { _down = b; }
    public void pass(boolean b)     { _pass = b; }
    public boolean pass()           { return _pass; }

    public void step() {
        int minX;
        int minY;
        if(_right) {
            minX = _x;
            _x++;
        } else {
            _x--;
            minX = _x;
        }
        if(_down) {
            minY = _y;
            _y++;
        } else {
            _y--;
            minY = _y;
        }
        _stage.repaint(0, minX - RADIUS, minY - RADIUS, DIAMETER + 1, DIAMETER + 1);
    }

    public void moveTo(int x, int y) {
        int minX = Math.min(_x, x);
        int minY = Math.min(_y, y);
        int deltaX = Math.max(_x, x) - minX;
        int deltaY = Math.max(_y, y) - minY;
        _x = x;
        _y = y;
        _stage.repaint(0, minX - RADIUS, minY - RADIUS, deltaX + DIAMETER, deltaY + DIAMETER);
    }

    public void draw(Graphics g) {
        g.setColor(Color.ORANGE);
        g.fillOval(_x - RADIUS, _y - RADIUS, DIAMETER, DIAMETER);
        g.setColor(_pass ? _passColor : _normalColor);
        g.fillOval(_x - RADIUS + 2, _y - RADIUS + 2, DIAMETER - 4, DIAMETER - 4);
    }

    private Stage   _stage = Stage.Instance();
    // position
    private int     _x;
    private int     _y;
    // direction, initially going up and right
    private boolean _right = true;
    private boolean _down = false;
    // a passing bal 
    private boolean _pass = false;
    // colors
    private Color   _normalColor = Color.YELLOW;
    private Color   _passColor = Color.RED;
}
